package Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Statement;
import java.util.ArrayList;

import data.DataConn;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.showContent;

public class view extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public view() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		//response.setContentType("text/html;charset=utf-8");
		//PrintWriter out = response.getWriter();
		try {
			int id = Integer.parseInt(request.getParameter("id"));
			//String id = request.getParameter("id");
			DataConn conn = new DataConn();
			Statement statement = conn.getConn("root", "root", "test").createStatement();
			statement.executeUpdate("update `content` set HitsNum = HitsNum +1 where id="+id);
			statement.close();
			showContent cont  = new showContent();
			ArrayList content = cont.viewContent(id);
			request.setAttribute("content", content);
			ServletContext application = this.getServletContext();
			RequestDispatcher rd = application.getRequestDispatcher("/view.jsp");
			rd.forward(request, response);
		}catch(Exception e) {
			e.printStackTrace();
		}
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request,response);
	}

	public void init() throws ServletException {
		// Put your code here
	}

}
